package org.micropayment.model.service.jpa;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.micropayment.model.MicropaymentUser;
import org.micropayment.model.service.MicropaymentUserDAO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 *
 * @author jj
 */
@Repository("userDao")
public class JpaMicropaymentUserDAO implements MicropaymentUserDAO {

    private static Logger logger =
            LoggerFactory.getLogger(JpaMicropaymentUserDAO.class);

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    public MicropaymentUser findById(Integer id) {
        return entityManager.find(MicropaymentUser.class, id);
    }

    public List<MicropaymentUser> findAll() {
        Query query = 
                entityManager.createQuery("select u from MicropaymentUser u");
        List<MicropaymentUser> users = null;
        users = query.getResultList();
        return users;
    }

    public MicropaymentUser findByUserId(String userId) {
    	MicropaymentUser result = null;
        Query query = entityManager.createNamedQuery("MicropaymentUser.findByUserid");
        query.setParameter("userid", userId);
        List<MicropaymentUser> users = query.getResultList();
        if (!users.isEmpty()) result = users.get(0);
        return result;
    }

    public List<MicropaymentUser> findByPartialName(String partName) {
        Query query =
                entityManager.createQuery("select c from MicropaymentUser c where c.name LIKE :partName");
        //query.setParameter("partName", "%" + partName.toUpperCase() + "%");
        query.setParameter("partName", "%" + partName + "%");
        List<MicropaymentUser> users = null;
        users = query.getResultList();
        return users;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public MicropaymentUser save(MicropaymentUser user) {
        entityManager.persist(user);
        entityManager.flush();
        return user;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public MicropaymentUser update(MicropaymentUser user) {
        entityManager.merge(user);
        entityManager.flush();
        return user;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public boolean delete(MicropaymentUser user) {
        user = entityManager.getReference(MicropaymentUser.class, user.getId());
        if (user == null) return false;
        entityManager.remove(user);
        entityManager.flush();
        return true;
    }

    public List<MicropaymentUser> findList(int firstResult, int maxResults) {
        Query query = entityManager.createQuery(
                "SELECT d FROM MicropaymentUser d ORDER BY dateAdded DESC");
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List<MicropaymentUser> users = query.getResultList();
        return users;
    }

}
